package cn.tedu.micro.demo.all.controller;

import cn.tedu.micro.demo.all.dao.repository.CartRepository;
import cn.tedu.micro.demo.all.service.CartService;
import cn.tedu.micro.demo.pojo.po.CartPO;
import cn.tedu.micro.demo.pojo.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.JsonResult;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private CartService cartService;
    @PostMapping("/add")
    public JsonResult addCart(String userId,String productCode,Integer count){
        CartPO cartPO = new CartPO();
        cartPO.setUserId(userId);
        cartPO.setProductCode(productCode);
        cartPO.setCount(count);
        cartRepository.save(cartPO);
        return JsonResult.ok();
    }
    @GetMapping("/list")
    public JsonResult listCart(String userId){
        List<CartVO> cartVOList = cartService.listCart(userId);
        return JsonResult.ok("ok",cartVOList);
    }
}
